package com.xy.netty.dome.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author niuniu
 */
@Slf4j
public class EchoServerDelimiterHandler extends ChannelInboundHandlerAdapter {
    private  int countNum=0;
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //第一种
        //Channel channel = ctx.channel();
        //channel.writeAndFlush(Unpooled.copiedBuffer("发送信息测试",CharsetUtil.UTF_8));

        //第二种
        //ChannelPipeline channelPipeline = ctx.pipeline();
        //channelPipeline.writeAndFlush(Unpooled.copiedBuffer("发送信息测试",CharsetUtil.UTF_8));
//        String sendMsg="服务端发送信息测试";
//        ctx.channel().writeAndFlush(Unpooled.copiedBuffer(sendMsg,CharsetUtil.UTF_8));
//        log.info("服务端发送数据：{}",sendMsg);
        String data = (String) msg;
        countNum ++;
        log.info("服务端收到数据：{},接收信息序列号：{}",data,countNum);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        log.info("EchoServerDelimiterHandler channelReadComplete");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("EchoServerDelimiterHandler exceptionCaught",cause);
        ctx.close();
    }


    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        log.info("EchoServerDelimiterHandler channelRegistered");
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        log.info("EchoServerDelimiterHandler channelUnregistered");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("EchoServerDelimiterHandler channelActive");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("EchoServerDelimiterHandler channelInactive");
    }
}
